package org.dromara.system.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.SkyHumanDishonest;
import org.dromara.system.domain.bo.ArchivesBo;
import org.dromara.system.utils.SkyEyeClientUtil;
import org.dromara.system.utils.SkyEyeProperties;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SkyAbnornalBo;
import org.dromara.system.domain.vo.SkyAbnornalVo;
import org.dromara.system.domain.SkyAbnornal;
import org.dromara.system.mapper.SkyAbnornalMapper;
import org.dromara.system.service.ISkyAbnornalService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 经营异常和严重违法Service业务层处理
 *
 * @author Lion Li
 * @date 2024-09-20
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class SkyAbnornalServiceImpl implements ISkyAbnornalService {

    private final SkyAbnornalMapper baseMapper;

    /**
     * 查询经营异常和严重违法
     */
    @Override
    public SkyAbnornalVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询经营异常和严重违法列表
     */
    @Override
    public TableDataInfo<SkyAbnornalVo> queryPageList(SkyAbnornalBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SkyAbnornal> lqw = buildQueryWrapper(bo);
        Page<SkyAbnornalVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询经营异常和严重违法列表
     */
    @Override
    public List<SkyAbnornalVo> queryList(SkyAbnornalBo bo) {
        LambdaQueryWrapper<SkyAbnornal> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SkyAbnornal> buildQueryWrapper(SkyAbnornalBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SkyAbnornal> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDataType() != null, SkyAbnornal::getDataType, bo.getDataType());
        lqw.eq(bo.getPutDate() != null, SkyAbnornal::getPutDate, bo.getPutDate());
        lqw.eq(bo.getRemoveDate() != null, SkyAbnornal::getRemoveDate, bo.getRemoveDate());
        return lqw;
    }

    /**
     * 新增经营异常和严重违法
     */
    @Override
    public Boolean insertByBo(SkyAbnornalBo bo) {
        SkyAbnornal add = MapstructUtils.convert(bo, SkyAbnornal.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改经营异常和严重违法
     */
    @Override
    public Boolean updateByBo(SkyAbnornalBo bo) {
        SkyAbnornal update = MapstructUtils.convert(bo, SkyAbnornal.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SkyAbnornal entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除经营异常和严重违法
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 查询天眼查数据
     * @param bo 查询条件
     * @param path 路径
     * @param type 类型
     * @return 返回list
     */
    private List<JSONObject> queryAbnormal(ArchivesBo bo, String path, String type) {
        JSONObject jo = SkyEyeClientUtil.query(bo, path);

        if (jo.isEmpty() || jo.getInt("total") == 0) {
            return new ArrayList<>();
        }

        return toArray(jo.getJSONArray("items"), type);
    }

    /**
     * 数据转list
     * @param array 数据集
     * @param type 类型
     * @return 类型
     */
    private List<JSONObject> toArray(JSONArray array, String type) {
        return array.stream().map(data -> {
            JSONObject jo3 = (JSONObject) data;
            jo3.set("dataType", type);
            return jo3;
        }).toList();
    }
}
